package org.classdef.analyzer.info.field;

import org.classdef.analyzer.info.ClassDef;
import org.classdef.analyzer.info.accesstype.FieldAccessType;
import org.classdef.analyzer.info.attribute.AttributePool;
import org.classdef.analyzer.info.constant.ConstantInfo;
import org.classdef.analyzer.info.fieldtype.FieldType;

/**
 * 
 * 字段描述信息
 *
 */
public class FieldInfo {
	
	private short accessFlags;
	private short nameIndex;
	private short descriptorIndex;
	private short attributesCount;
	private AttributePool attributePool;
	
	protected ClassDef def;

	public FieldInfo(short accessFlags, short nameIndex, short descriptorIndex,
			short attributesCount, AttributePool attributePool) {
		this.accessFlags = accessFlags;
		this.nameIndex = nameIndex;
		this.descriptorIndex = descriptorIndex;
		this.attributesCount = attributesCount;
		this.attributePool = attributePool;
	}

	/**
	 * @return accessFlags
	 */
	public short getAccessFlags() {
		return accessFlags;
	}

	/**
	 * @return nameIndex
	 */
	public short getNameIndex() {
		return nameIndex;
	}

	/**
	 * @return descriptorIndex
	 */
	public short getDescriptorIndex() {
		return descriptorIndex;
	}

	/**
	 * @return attributesCount
	 */
	public short getAttributesCount() {
		return attributesCount;
	}

	/**
	 * @return attributePool
	 */
	public AttributePool getAttributePool() {
		return attributePool;
	}
	
	public String getDescriptor() {
		return FieldType.getType(getConst(descriptorIndex).getValue().toString());
	}
	
	public String getDefine(){
		return FieldAccessType.getType(accessFlags) + " " + getDescriptor() + " " + getConst(nameIndex).getValue() + ";";
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		sb.append("\"accessFlags\":").append(accessFlags);
		sb.append(",\"access\":\"").append(FieldAccessType.getType(accessFlags)).append("\"");
		sb.append(",\"nameIndex\":").append(nameIndex);
		sb.append(",\"name\":\"").append(getConst(nameIndex).getValue()).append("\"");
		sb.append(",\"descriptorIndex\":").append(descriptorIndex);
		sb.append(",\"descriptor\":\"").append(getDescriptor()).append("\"");
		sb.append(",\"attributesCount\":").append(attributesCount);
		sb.append(",\"attributePool\":").append(attributePool);
		sb.append("}");
		return sb.toString();
	}
	
	private ConstantInfo getConst(int index){
		return def.getConstantPool().get(index);
	}

}